<?php
    session_start();
    if(isset($_POST['type'])){
        if($_POST['type']=='M'){
            $title=$_POST['title'];
            $firstname=$_POST['firstname'];
            $lastname=$_POST['lastname'];
            $email=$_POST['email'];
            $phoneNo=$_POST['phoneNo'];
            $position=$_POST['position'];
            $staffname=$_POST['staffname'];
            modify_staff($title,$firstname,$lastname,$email,$phoneNo,$position,$staffname);
        }
        else if($_POST['type']=='staff_C'){
            $title=$_POST['title'];
            $firstname=$_POST['firstname'];
            $lastname=$_POST['lastname'];
            $email=$_POST['email'];
            $phoneNo=$_POST['phoneNo'];
            $position=$_POST['position'];
            $staffname=$_POST['staffname'];
            $totalhours=$_POST['totalhours'];
            creat_staff($title,$firstname,$lastname,$email,$phoneNo,$position,$staffname,$totalhours);
            header("location:./AddStaff.php");
        }
    }
    else{
        echo"error:101";
    }
    function creat_staff($title,$firstname,$lastname,$email,$phoneNo,$position,$staffname,$totalhours){
        include 'sqlconnect.php';
        if(mysqli_fetch_array(mysqli_query($conn,"SELECT *FROM staff WHERE username='$staffname'"))){//judge the username isset or not
            $sql="SELECT * FROM staff WHERE username='$staffname'";
            $result=mysqli_query($conn,$sql);
            while($row=mysqli_fetch_array($result)){//judge this username is created or not
                $r_email=$row['email'];
            }
            if($r_email==null){//email is null,prove this username isn't created
                $sql="UPDATE staff SET firstname='$firstname',lastname='$lastname',title='$title',
                email='$email',phoneNo='$phoneNo',idposition=$position,totalWLUs=$totalhours 
                WHERE username='$staffname'";
                mysqli_query($conn,$sql);
                $_SESSION['creat_reshow']='success';
                $_SESSION['staff']=$firstname." ".$lastname;
                //echo"<div class='success-modify'>The staff $firstname $lastname creats successfull </div>";
                mysqli_close($conn);
            }
            else{//this staff has created
                $_SESSION['creat_reshow']='fail_had';
                $_SESSION['staff']=$firstname." ".$lastname;
                //echo"<div class='fail-modify'>The staff $firstname $lastname had created</div>";
            } 
        }else{//this staff isn't exit
            $_SESSION['creat_reshow']='fail_exit';
            $_SESSION['staff']=$staffname;
            //echo"<div class='fail-modify'>The username $staffname isn't exit";
        }
    }
    function modify_staff($title,$firstname,$lastname,$email,$phoneNo,$position,$staffname){
        include 'sqlconnect.php';
        $sql="UPDATE staff SET title='$title',firstName='$firstname',lastName='$lastname', 
        email='$email',phoneNo='$phoneNo',idposition='$position' WHERE username='$staffname'";
        mysqli_query($conn,$sql);
        $sql="SELECT *FROM staff where username='$staffname'";
        $resutl=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($resutl)){
            $username=$row['username'];
            $title=$row['title']." ".$row['firstName']." ".$row['lastName'];
            if($row['idposition']==1){
                $position='Head of Department';
            }
            else if($row['idposition']==2){
                $position='Secretory';
            }
            else if($row['idposition']==3){
                $position='Academic Staff';
            }
            $email=$row['email'];
            $phone=$row['phoneNo'];
            $totalWLUs=$row['totalWLUs'];
            echo"
                <div class='success-modify' >The data has modified successfull!</div>
                <p>
                    <div class='staff-list-button-group'>
                        <p><button class='btn btn-default st_button' data-username='$username' data-type='M'>Modify</button></p>
                        <p><button class='btn btn-default st_button' data-username='$username' data-type='D'>Delete</button></p>
                        <p><button class='btn btn-default st_button' data-username='$username' data-type='V'>View tasks</button></p>
                    </div>
                <b>$title</b><br/>
                <b>Position: </b>$position<br/>
                <b>Email: </b>$email<br/>
                <b>Work Phone: </b>$phone<br/>
                <b>Total WLUs: </b>$totalWLUs/1600
                </p>
                <br/>";
            mysqli_close($conn);
        }
        
    }
?>